var test = 0;
$(document).ready(function() {
	$('#submitbutton').click(function() {
		shortestpath();
		console.log("test : " + test);
	});
	
	$('#titletimduong').click(function() {
		$(".info").slideToggle("slow");
		if($(".info").is(':visible')){
			//
		}
	});
	
	$("#dep").keyup(function(){
		searchStation("#dep", $("#dep").val());
    });
	$("#arr").keyup(function(){
		searchStation("#arr", $("#arr").val());
    });
	
	$("#opacityMap, #cancelOption").click(function() {
		$("#option").hide();
		$("#opacityMap").hide()
	});
	
	$("#depOption").click(function() {
		var valuSelect = $("input[name='selectS']:checked").val();
		$("#dep").prop('title', valuSelect);
		var textDep = $("#"+valuSelect).text();
		textDep = textDep.substr(2, textDep.length - 1);
		$("#dep").val(textDep);
		addmarker(lat, lon, "images/layers.png", 
				"<b>Điểm Đi!</b><br>" + textDep + ".", 1);
		$("#option").hide();
		$("#opacityMap").hide()
	});
	
	$("#arOption").click(function() {
		var valuSelect = $("input[name='selectS']:checked").val();
		$("#arr").prop('title', valuSelect);
		var textArr = $("#"+valuSelect).text();
		textArr = textArr.substr(2, textArr.length - 1);
		$("#arr").val(textArr);
		addmarker(lat, lon, "images/2.png", 
				"<b>Điểm Đến!</b><br>" + textArr + ".", 2);
		$("#option").hide();
		$("#opacityMap").hide()
	});
});

function searchStation(id, str){
	$(id).autocomplete({
		source: function (request, response)
	    {
	        $.ajax(
	        {
	            url: "api/json/station/textsearch/str=" + str,
	            data:
	            {
	                term: request.term,
	            },
	            success: function (data)
	            {
	            	response(JSON.parse(data));
	            }
	        });
	    },
		autoFocus : true,
		minLength : 2,
		select: function (event, ui){
			console.log(ui);
			$(id).prop('title', ui.item.id);
			if(id == "#dep"){
				addmarker(ui.item.lat, ui.item.lon, "images/layers.png", 
						"<b>Điểm Đi!</b><br>" + ui.item.value + ".", 1);
			}
			else{
				addmarker(ui.item.lat, ui.item.lon, "images/layers.png", 
						"<b>Điểm Đến!</b><br>" + ui.item.value + ".", 2);
			}
		}
	});
}

function clickSerchStation(lat, lon){
	
	var strrequest = "api/json/station/clicksearch/lat=" + lat + "&lon=" + lon;
	$.ajax({
		type : "GET",
		url : strrequest,
		success : function(data) {
			console.log(JSON.parse(data));
			var dt = JSON.parse(data);
			$("#stationOption").html(null);
			for(var i = 0; i < dt.length; i++){
				htmlStr = "<label><input type=\"radio\" name=\"selectS\" value=\"" 
				+ dt[i].id + "\"><p id=\"" + dt[i].id 
				+ "\" style=\"display: inline; cursor:pointer;\">" 
				+ "&nbsp;&nbsp;" + dt[i].value + "</p></label><br>";
				$("#stationOption").append(htmlStr);
			}
		}
	});
}

function shortestpath() {
	var strrequest = "api/json/shortestpath/";
	var dep = "dep=" + $('#dep').prop('title');
	var arr = "&arr=" + $('#arr').prop('title');
	var fp = "&fp=" + $("#firstPriority").val();
	var sp = "&sp=" + $("#secondPriority").val();
	var tp = "&tp=" + $("#thirdPriority").val();
	var minwalk, maxwalk, walk;
	if (parseInt($("#walk").val()) == 0) {
		minwalk = 0;
		maxwalk = 0;
		walk = "&minwalk=" + minwalk + "&maxwalk=" + maxwalk;
	} else {
		minwalk = parseInt($("#oncewalk").val());
		maxwalk = parseInt($("#maxwalk").val());
		if (minwalk > maxwalk) {
			alert("minwalk must less than maxwalk");
		} else {
			walk = "&minwalk=" + minwalk + "&maxwalk=" + maxwalk;
		}
	}
	var timedep = "&timedep="
			+ (parseInt($("#giodi").val()) * 60 + parseInt($("#phutdi").val()));

	strrequest = strrequest + dep + arr + fp + sp + tp + walk + timedep;
	alert(strrequest);
	$.ajax({
		type : "GET",
		url : strrequest,
		success : function(data) {
			//$("#map").prepend(data);
			console.log(JSON.parse(data));
			//$("#content").val(JSON.stringify(data));
			console.log("test set data");
			test = JSON.parse(data);
			console.log("test : " + test);
		}
	});
}